<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace templates;

defined('ANS') or die();

class Templates {
	private $Debug;

	public function __construct () {
		global $Debug;

		$this->Debug = $Debug;
	}


	/**
	 * public function load (string $template, [boolean $empty_template], [boolean $debug])
	 *
	 * Load a template
	 *
	 * return boolean/string
	 */
	public function load ($template, $empty_template = true) {
		global $Config;

		$empty_template = $empty_template ? filePath('phpcan/includes|utils/empty_template.php') : false;
		$debug = false;
		$templates = $Config->templates;

		if ($templates[$template]) {
			if (strpos($templates[$template], '|') === false) {
				$templates[$template] = 'templates|'.$templates[$template];
			}

			$template = filePath($templates[$template]);

			$debug = true;
		} else if (isset($templates[$template])) {
			return $empty_template;
		} else if ($template) {
			if ($template[0] != '/') {
				if (strpos($template, '|') === false) {
					$template = 'templates|'.$template;
				}

				$template = filePath($template);
			}
		} else {
			return $empty_template;
		}

		return is_file($template) ? $template : $empty_template;
	}


	/**
	 * public function render (string $template, [array $data_content], [boolean $return_html], [string $separation], [string $after])
	 *
	 * Render a template
	 *
	 * return string/boolean
	 */
	public function render ($template, $data_content = array(), $return_html = false, $separation = null, $after = null) {
		$template = $this->load($template, false);

		if (!$template) {
			return false;
		}

		ob_start();
		$return = array();

		if (isNumericalArray($data_content)) {
			foreach ($data_content as $index => $data_content_value) {
				$data_content_value['index'] = $index;

				includeFile($template, $data_content_value);

				$return[] = ob_get_contents();
				ob_clean();
			}
		} else {
			includeFile($template, $data_content);

			$return[] = ob_get_contents();
		}

		ob_end_clean();

		if (is_null($separation)) {
			$return = implode($return);
		} else if (is_null($after)) {
			$return = implode($separation, $return);
		} else {
			$return = $separation.implode($after.$separation, $return).$after;
		}

		if (!$return_html) {
			echo $return;
			return true;
		}

		return $return;
	}


	/**
	 * public function exists (string $template)
	 *
	 * return boolean
	 */
	public function exists ($template) {
		return ($this->load($template, false) ? true : false);
	}

	/**
	 * public function add (string $name, string $file, [string $exit_mode])
	 *
	 * return none
	 */
	public function add ($name, $file, $exit_mode = '') {
		global $Config, $Vars;

		if (!$exit_mode || $exit_mode == 'all' || $Vars->getExitMode($exit_mode)) {
			$Config->templates[$name] = $file;
		}
	}

	/**
	 * public function remove ($name, [string $exit_mode])
	 *
	 * return none
	 */
	public function remove ($name, $exit_mode = '') {
		global $Config, $Vars;

		if (!$exit_mode || $exit_mode == 'all' || $Vars->getExitMode($exit_mode)) {
			unset($Config->templates[$name]);
		}
	}
}
?>
